package com.example.exchange.controller;

import cn.hutool.json.JSONObject;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api")
public class DeviceDataController {

    @Autowired
    private RabbitTemplate rabbitTemplate;



    public void sendMessage(Object message) {
        Map<String,Object> map = (HashMap<String,Object>)message;
        log.info("传感器所有数据:{}",map);

        Map<String,Object>payload = (Map<String, Object>)map.get("payload");
        log.info("传感器采集数据:{}",payload);

        for (String string : payload.keySet()) {
            boolean equals = payload.get(string).equals(null);
            if (equals){
                payload.put(string,-500);
            }
        }
        map.put("payload",payload);



//        ExecutorService executorService = Executors.newFixedThreadPool(2);
        // 创建两个线程，分别一个线程发消息 一个线程QC质控
//        executorService.submit(() -> {
            rabbitTemplate.convertAndSend("deviceExchange", "deviceRoutingKey", message);
//            rabbitTemplate.convertAndSend("deviceExchange", "deviceInfo", deviceInfo);
//        });
//        executorService.submit(() -> {
//
////            sendMessage(dataMap);
//        });
        //   等待所有线程完成
//        executorService.shutdown();

    }




    @PostMapping("/device/status/online")
    public void deviceStatusOnline(@RequestBody JSONObject object){


        JSONObject request = new JSONObject();
        request.set("deviceId",object.get("clientid"));
        request.set("eventType",1);
        request.set("timestamp",object.get("timestamp"));

        this.sendMessageDeviceStatus(request);
    }
    @PostMapping("/device/status/offline")
    public void deviceStatusOffline(@RequestBody JSONObject object){

        JSONObject request = new JSONObject();
        request.set("deviceId",object.get("clientid"));
        request.set("eventType",0);
        request.set("timestamp",object.get("timestamp"));

        this.sendMessageDeviceStatus(request);
    }

    @PostMapping("/device/cmd/log")
    public void deviceCmdLog(@RequestBody JSONObject object){
        JSONObject payload = new JSONObject(object.get("payload"));
        log.info("设备指令回应:{}",object);
        log.info("设备指令回应:{}",payload);

//        this.sendMessageDeviceStatus(object);
    }


    public void sendMessageDeviceStatus(JSONObject message) {

//        Map<String,Object> map = (HashMap<String,Object>)message;
//        log.info("传感器状态数据:{}",map);


        rabbitTemplate.convertAndSend("deviceExchange", "deviceStatus", message);
    }


    @PostMapping("/data")
    public void deviceData(@RequestBody Object object){

        sendMessage(object);
    }


    @PostMapping("/data2")
    public void deviceData2(@RequestBody JSONObject json){


        JSONObject payload = new JSONObject(json.get("payload"));

        JSONObject properties = new JSONObject( payload.get("properties"));
       
        JSONObject baseData = new JSONObject( properties.get("BaseData"));
        baseData.set("timestamp",payload.get("timestamp"));
//        sendMessage(baseData);

        //设备信息更新
        Map<String,Object> deviceInfo = new HashMap<>();
        deviceInfo.put("deviceNo",payload.get("deviceId")); //sn码
        deviceInfo.put("longitude",payload.get("longitude")); //经度
        deviceInfo.put("latitude",payload.get("latitude")); //纬度
        deviceInfo.put("altitude",payload.get("altitude")); //海拔高度

        //设备数据更新
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("deviceId",json.get("deviceId"));
        dataMap.put("payload",baseData);
//        // 创建线程池
//        ExecutorService executorService = Executors.newFixedThreadPool(2);
//        // 创建两个线程，分别一个线程发消息 一个线程QC质控
//        executorService.submit(() -> {
            rabbitTemplate.convertAndSend("deviceExchange", "deviceInfo", deviceInfo);
//        });
//        executorService.submit(() -> {
//
////            sendMessage(dataMap);
//        });
//      //   等待所有线程完成
//        executorService.shutdown();

    }

    @PostMapping("/emqx/data")
    public void deviceEMQXData2(@RequestBody JSONObject json){

        log.info("emqx转发数据");
        log.info(json.toString());

        JSONObject payload = new JSONObject(json.get("payload"));
        JSONObject properties = new JSONObject( payload.get("properties"));

        JSONObject baseData = new JSONObject( properties.get("BaseData"));
        baseData.set("timestamp",payload.get("timestamp"));

        //设备信息更新
        Map<String,Object> deviceInfo = new HashMap<>();
        deviceInfo.put("deviceNo",payload.get("deviceId")); //sn码
        deviceInfo.put("longitude",payload.get("longitude")); //经度
        deviceInfo.put("latitude",payload.get("latitude")); //纬度

        deviceInfo.put("altitude",payload.get("altitude")); //海拔高度
        if (payload.get("altitude").equals(null)){
            deviceInfo.put("altitude",-500); //海拔高度
        }


        //设备数据更新
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("deviceId",payload.get("deviceId"));

        dataMap.put("payload",baseData);

        rabbitTemplate.convertAndSend("deviceExchange", "deviceInfo", deviceInfo);

        sendMessage(dataMap);
    }
    @GetMapping("/hi")
    public String deviceData2(){

        return "hi";
    }



}
